package com.ayuku.course.report;

import net.sf.paperclips.DefaultGridLook;
import net.sf.paperclips.GridPrint;
import net.sf.paperclips.LinePrint;
import net.sf.paperclips.Print;
import net.sf.paperclips.TextPrint;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Shell;

import com.ayuku.commons.SqliteDB;
import com.ayuku.commons.ReportViewerPaperClips;
import com.ayuku.commons.Util;
import com.ayuku.course.persistence.model.Employee;

public class EmployeeReport extends ReportViewerPaperClips {

	private FontData fontData = new FontData("Arial", 6, SWT.NORMAL);
	private FontData fontBold = new FontData("Arial", 6, SWT.BOLD);
	private FontData fontHeader = new FontData("Arial", 8, SWT.BOLD);
	private FontData fontSpace = new FontData("Arial", 6, SWT.NORMAL);

	private java.util.List<Employee> lst;

	public EmployeeReport(Shell parent, int style) {
		super(parent, style);

	}

	private java.util.List<Employee> getDataFromServer() {
		java.util.List<Employee> lst = null;
		try {
			lst = SqliteDB
			.getPersist()
			.readList(
					Employee.class,
					"SELECT * FROM employee WHERE code LIKE ? or name LIKE ? LIMIT 1000",
					new Object[] { "%%", "%%"});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lst;
	}

	@Override
	public Print createBody() {
		GridPrint mainGrid = new GridPrint("d", new DefaultGridLook(1, 1));

		createDetail(mainGrid);
		return mainGrid;
	}

	private void createDetail(GridPrint mainGrid) {

		GridPrint grid = new GridPrint("40,120,100, 80,80, 200",
				new DefaultGridLook(1, 1));

		grid.add(SWT.CENTER, new TextPrint(getReportTitle(), fontHeader,
				SWT.CENTER), GridPrint.REMAINDER);

		grid.add(new TextPrint("", fontSpace), GridPrint.REMAINDER);
		grid.add(new LinePrint(SWT.HORIZONTAL, 0.2), GridPrint.REMAINDER);

		// ==create judul kolom
		grid.add(new TextPrint("Emp ID", fontBold));
		grid.add(new TextPrint("Name", fontBold));
		grid.add(new TextPrint("Phone", fontBold));
		grid.add(new TextPrint("Join date", fontBold));
		grid.add(new TextPrint("Level", fontBold));
		grid.add(new TextPrint("Address", fontBold));		
		// end off

		grid.add(new LinePrint(SWT.HORIZONTAL, 0.2), GridPrint.REMAINDER);

		lst = getDataFromServer();
		if (lst != null && lst.size() > 0) {
			for (int j = 0; j < lst.size(); j++) {
				Employee kpg = lst.get(j);
				grid.add(SWT.LEFT,
						new TextPrint(Util.nullToString(kpg.getCode()),
								fontData));
				grid.add(SWT.LEFT,
						new TextPrint(Util.trimString(kpg.getName(), 30),
								fontData));
				grid.add(SWT.LEFT,
						new TextPrint(Util.nullToString(kpg.getPhone()),
								fontData));
				grid.add(SWT.LEFT,
						new TextPrint(Util.dateToString(kpg
								.getJoinDate()), fontData));
				grid.add(SWT.LEFT,
						new TextPrint(Util.nullToString(kpg.getLevel()),
								fontData));
				grid.add(SWT.LEFT,
						new TextPrint(Util.nullToString(kpg.getAddress()),
								fontData));
			}
		}
		mainGrid.add(grid);
	}

	@Override
	public String getReportTitle() {
		return "Employee Report";
	}

}
